<?php

namespace App\Exports;

use App\Models\Order;
use Maatwebsite\Excel\Concerns\FromArray;

class OrderExport implements FromArray
{
    /**订单导出**/
    private $params;
    /**
     * @var string[] 导出标题集合
     */
    private $headingList = [
        'id'=>'ID',
        'order_number'=>'订单编号',
        'customer_name'=>'客户名称',
        'serial_number'=>'订单流水号',
        'product_name'=>'产品名称',
        'model_number'=>'型号',
        'specification'=>'规格',
        'color'=>'颜色',
        'direction'=>'方向',
        'remark'=>'备注',
        'order_date'=>'接单日期',
        'delivery_date'=>'交货日期'
    ];



    public function __construct($params) {
        ob_end_clean();
        $this->params = $params;
    }




    /**
     * Notes: 处理导出数据表头
     * User: q
     * DateTime: 2021/10/13 18:10
     * @return array
     */
    public function headings(): array{

        return  [
            "ID",
            "订单编号",
            "客户名称",
            "订单流水号",
            "产品名称",
            "型号",
            "规格",
            "颜色",
            "方向",
            "备注",
            "接单日期",
            "交货日期"
        ];
    }


    public function array() : array{
        $data[] = $this->headingList;
        $params = $this->params;
        if($params['order_name'] == '订单列表'){
            $model = Order::Search($params);
        }elseif($params['order_name'] == '订单审核'){
            $model = Order::Audit($params);
        }elseif($params['order_name'] == '订单进度'){
            $model = Order::Schedule($params);
        }
        $array = $model->with(['color'=>function($q){
            return $q->select('id','color');
        },'product'=>function($q){
            return $q->select('id','product_name')->with('job_type');
        },'customer'=>function($q){
            return $q->select('id','customer_name','customer_address');
        },'modelNumber'=>function($q){
            return $q->select('id','model_number')->with('specification');
        },'orderChild'=>function($q){
            return $q->with(['job_type','employee']);
        },'specification'=>function($q){
            return $q->select('id','specification');
        }])
            ->orderBy('id','desc')->get()->toArray();
        foreach ($array as $key=>&$val){
            $data[] = [
                'id' => $val['id'],
                'order_number' => $val['order_number']??"",
                'customer_name' => $val['customer']['customer_name']??'',
                'serial_number' => $val['serial_number']??'',
                'product_name' => $val['product']['product_name']??'',
                'model_number' => $val['model_number']['model_number']??'',
                'specification' => $val['specification']['specification']??'',
                'color' => $val['color']['color']??'',
                'direction' => $val['direction']??'',
                'remark' => $val['remark']??'',
                'order_date' => $val['order_date']??'',
                'delivery_date' => $val['delivery_date']??'',
            ];
        }
        return $data;
    }


}
